package com.unience.ec2.eclipse.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.ui.part.ViewPart;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;

import com.unience.ec2.eclipse.config.Config;
import com.unience.ec2.eclipse.config.UtilSWT;
import com.xerox.amazonws.ec2.AddressInfo;
import com.xerox.amazonws.ec2.EC2Exception;
import com.xerox.amazonws.ec2.Jec2;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ElasticIPViewPart extends ViewPart {
    public static final String ID_VIEW =
        "com.unience.ec2.eclipse.view.ElasticIPViewPart"; //$NON-NLS-1$

    Composite composite1;
    private ToolItem toolItem1;
    private Table tableElasticIP;
    private ToolBar toolBar;

    /**
     * 
     */
    public ElasticIPViewPart() {
        super();
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    public void createPartControl(Composite parent) {
    	{
	    	parent.setSize(956, 600);
	    	{
	    		composite1 = new Composite(parent, SWT.NULL);
	    		GridLayout composite1Layout = new GridLayout();
	    		composite1Layout.makeColumnsEqualWidth = true;
	    		composite1Layout.horizontalSpacing = 1;
	    		composite1.setLayout(composite1Layout);
	    		{
	    			toolBar = new ToolBar(composite1, SWT.NONE);
	    			toolBar.setBounds(5, 5, 14, 14);
	    			{
	    				
	    				toolItem1 = new ToolItem(toolBar, SWT.NONE);	    				
	    				toolItem1.setImage(UtilSWT.getImage("refresh.png"));
	    				toolItem1.setText("refresh");
	    				
	    				toolItem1 = new ToolItem(toolBar, SWT.NONE);
	    				toolItem1.setImage(UtilSWT.getImage("action_add.png"));
	    				toolItem1.setText("new");
	    				
	    				toolItem1 = new ToolItem(toolBar, SWT.NONE);
	    				toolItem1.setImage(UtilSWT.getImage("action_remove.png"));
	    				toolItem1.setText("delete");
	    				
	    				toolItem1 = new ToolItem(toolBar, SWT.NONE);
	    				toolItem1.setImage(UtilSWT.getImage("plugin_add.png"));
	    				toolItem1.setText("associate");
	    				
	    				toolItem1 = new ToolItem(toolBar, SWT.NONE);
	    				toolItem1.setImage(UtilSWT.getImage("plugin_delete.png"));
	    				toolItem1.setText("deassociate");
	    				
	    				
	    			}
	    		}
	    		{
	    			GridData tableElasticIPLData = new GridData();
	    			tableElasticIPLData.widthHint = 844;
	    			tableElasticIPLData.heightHint = 264;
	    			tableElasticIP = new Table(composite1, SWT.NONE);
	    			tableElasticIP.setLayoutData(tableElasticIPLData);
	    			TableColumn tc1 = new TableColumn(tableElasticIP, SWT.CENTER);
	                TableColumn tc2 = new TableColumn(tableElasticIP, SWT.CENTER);
	                
	                tc1.setText("IP Address");
	                tc2.setText("InstanceID");
	                
	                tc1.setWidth(180);
	                tc2.setWidth(180);
	                
	                tableElasticIP.setHeaderVisible(true);
	                
	                
	    		}
	    	}
    	}
    	
    	loadAddress();
    	
    }
    
    private void loadAddress(){
    	
    	Config config = Config.getInstance();
    	Jec2 jec2 =  config.getEc2Instance().getEc2();
    	
    	try {
			List<AddressInfo> addresses = jec2.describeAddresses(new ArrayList<String>());
			
			for (AddressInfo addressInfo : addresses) {
				
				TableItem item = new TableItem(tableElasticIP, SWT.NONE);
				item.setText(new String[] {addressInfo.getPublicIp(), addressInfo.getInstanceId()});
				
				
			}
			
		} catch (EC2Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
     */
    public void setFocus() {
        // TODO Auto-generated method stub
    }
    
    /**
     * Cleans up all resources created by this ViewPart.
     */
    public void dispose() {
        super.dispose();
    }
    
}
